﻿using System;
using OpenTK;

namespace Painter3D.RenderSys.RenderObject
{
    /// <summary>
    /// 顶点数据的包围盒,通常意味着顶点数据中的三轴的最大最小值
    /// </summary>
    public struct BoundBox
    {
        readonly public Vector3 Low;
        readonly public Vector3 High;

        public BoundBox(Vector3 low, Vector3 high)
        {
            Low = low;
            High = high;
        }

        /// <summary>
        /// 从给定的位置数组计算包围盒
        /// </summary>
        /// <param name="positions">位置数组[x,y,z]...</param>
        public BoundBox(float[] positions)
        {
            Low = new Vector3(float.MaxValue);
            High = new Vector3(float.MinValue);
            for (int i = 0; i < positions.Length / 3; i++)
            {
                Low.X = Math.Min(positions[i * 3 + 0], Low.X);
                Low.Y = Math.Min(positions[i * 3 + 1], Low.Y);
                Low.Z = Math.Min(positions[i * 3 + 2], Low.Z);
                High.X = Math.Max(positions[i * 3 + 0], High.X);
                High.Y = Math.Max(positions[i * 3 + 1], High.Y);
                High.Z = Math.Max(positions[i * 3 + 2], High.Z);
            }
        }

        /// <summary>
        /// 返回包围盒的中心位置
        /// </summary>
        public Vector3 Central
        {
            get
            {
                return (Low + High) / 2;
            }
        }

        /// <summary>
        /// 返回包围盒各轴的宽度
        /// </summary>
        public Vector3 Width
        {
            get
            {
                return (High - Low);
            }
        }
    }
}
